<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

use backend\models\User;
use backend\models\Visitor;
use backend\models\Recharge;
use backend\models\UserConsume;
/**
 * User controller
 */
class UserController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
//                    [
//                        'actions' => ['login', 'captcha'],
//                        'allow' => true,
//                        'roles' => ['?'],
//                    ],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * 用户列表页
     *
     * @return string
     */
    public function actionReader() {
        $get = Yii::$app->request->get();
        $params = array(
            'page'      => isset( $get['page'] ) && intval($get['page']) ? $get['page'] : 1,
            'pageSize'  => 10,
            'search'    => isset( $get['search'] ) ? trim($get['search']) : '',
        );
        $user = new User();
        $data = $user->getList( $params );
        $data['model'] = $user;
        $data['select'] = $params;
        return $this->render('reader',$data);
    }
    
    /**
     * 游客列表页
     *
     * @return string
     */
    public function actionVisitor() {
        $get = Yii::$app->request->get();
        $params = array(
            'page'      => isset( $get['page'] ) ? $get['page'] : 1,
            'pageSize'  => isset( $get['pageSize'] ) ? $get['pageSize'] : 10,
            'start'     => isset( $get['start'] ) ? trim($get['start']) : '',
            'end'     => isset( $get['end'] ) ? trim($get['end']) : '',
        );
        $visitor = new Visitor();
        $data = $visitor->getVisitorList( $params );
        $data['model'] = $visitor;
        $data['select'] = $params;
        return $this->render('visitor',$data);
    }
    
    /**
     * 用户充值记录
     */
    public function actionRecharge() {
        $get = Yii::$app->request->get();
        if(!isset($get['uid'])){
            $res['name'] = '缺乏用户id';
            $res['message'] = '缺乏用户id';
            return $this->render('/site/error',$res);
        }
        $params = array(
            'page'      => isset( $get['page'] ) ? $get['page'] : 1,
            'pageSize'  => isset( $get['pageSize'] ) ? $get['pageSize'] : 10,
            'start'     => isset( $get['start'] ) ? trim($get['start']) : '',
            'end'     => isset( $get['end'] ) ? trim($get['end']) : '',
            'user_id'   => $get['uid'],
        );
        $recharge = new Recharge();
        $data = $recharge->getRechargeList( $params );
        $data['model'] = $recharge;
        $data['select'] = $params;
        return $this->render('recharge',$data);
    }
    
    /**
     * 用户消费记录
     */
    public function actionConsume() {
        $get = Yii::$app->request->get();
        if(!isset($get['uid'])){
            $res['name'] = '缺乏用户id';
            $res['message'] = '缺乏用户id';
            return $this->render('/site/error',$res);
        }
        $params = array(
            'page'      => isset( $get['page'] ) ? $get['page'] : 1,
            'pageSize'  => isset( $get['pageSize'] ) ? $get['pageSize'] : 10,
            'start'     => isset( $get['start'] ) ? trim($get['start']) : '',
            'end'     => isset( $get['end'] ) ? trim($get['end']) : '',
            'user_id'   => intval( $get['uid'] ),
        );
        $consume = new UserConsume();
        $data = $consume->getConsumeList( $params );
        $data['model'] = $consume;
        $data['select'] = $params;
        return $this->render( 'consume' , $data );
    }
    
    /**
     * 封号
     */
    public function actionDisabled() {
        $get = Yii::$app->request->get();
        if(!isset($get['uid'])){
            $res['name'] = '缺乏用户id';
            $res['message'] = '缺乏用户id';
            return $this->render('/site/error',$res);
        }
        $userModel = new User();
        $res = $userModel->disabledUser( intval( $get['uid'] ) );
        if( $res ){
            \Yii::$app->getSession()->setFlash('success', '操作成功');
        }else{
             \Yii::$app->getSession()->setFlash('error', '操作失败');
        }
        return $this->redirect('/user/reader');
    }
    
    /**
     * 解封
     */
    public function actionUndisabled() {
        $get = Yii::$app->request->get();
        if(!isset($get['uid'])){
            $res['name'] = '缺乏用户id';
            $res['message'] = '缺乏用户id';
            return $this->render('/site/error',$res);
        }
        $userModel = new User();
        $res = $userModel->undisabledUser( intval( $get['uid'] ) );
        if( $res ){
            \Yii::$app->getSession()->setFlash('success', '操作成功');
        }else{
             \Yii::$app->getSession()->setFlash('error', '操作失败');
        }
        return $this->redirect('/user/reader');
    }


}
